package com.example.bbs.service.impl;

import com.example.bbs.mapper.MessageMapper;
import com.example.bbs.pojo.Message;
import com.example.bbs.pojo.PageBean;
import com.example.bbs.service.MessageService;
import com.example.bbs.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MessageServiceImpl implements MessageService{
    @Autowired
    private MessageMapper messageMapper;
    @Override
    public PageBean<Message> list(Integer pageNum, Integer pageSize, String topic) {
        //1.创建PageBean对象
        PageBean<Message> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
        List<Message> as = messageMapper.list(topic);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        //将返回的数据根据页码和每页条数进行返回
        Page<Message> p = (Page<Message>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public Message getById(Integer id) {
//return messageMapper.selectById(id);
        return  messageMapper.getById(id);
    }
}
